package com.Interface.util;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.util.URIUtil;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;


/** 
* HTTP工具箱 
* 
* 
*/ 
public final class fHttpTookit1 { 

        /** 
         * 执行一个HTTP GET请求，返回请求响应的HTML 
         * 
         * @param url                 请求的URL地址 
         * @param queryString 请求的查询参数,可以为null 
         * @param charset         字符集 
         * @param pretty            是否美化 
         * @return 返回请求响应的HTML 
         */ 
        public static String doGet(String url, String queryString, String charset, boolean pretty) { 
                StringBuffer response = new StringBuffer(); 
                HttpClient client = new HttpClient(); 
                HttpMethod method = new GetMethod(url); 
                try { 
                        if (queryString.isEmpty()) 
                                //对get请求参数做了http请求默认编码，好像没有任何问题，汉字编码后，就成为%式样的字符串 
                                method.setQueryString(URIUtil.encodeQuery(queryString)); 
                        client.executeMethod(method); 
                        if (method.getStatusCode() == HttpStatus.SC_OK) { 
                                BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset)); 
                                String line; 
                                while ((line = reader.readLine()) != null) { 
                                        if (pretty) 
                                                response.append(line).append(System.getProperty("line.separator"));
                                        else 
                                                response.append(line); 
                                } 
                                reader.close(); 
                        } 
                } catch (URIException e) { 
                    //    log.error("执行HTTP Get请求时，编码查询字符串“" + queryString + "”发生异常！", e); 
                } catch (IOException e) { 
                     //   log.error("执行HTTP Get请求" + url + "时，发生异常！", e); 
                } finally { 
                        method.releaseConnection(); 
                } 
                return response.toString(); 
        } 

        /** 
         * 执行一个HTTP POST请求，返回请求响应的HTML 
         * 
         * @param url         请求的URL地址 
         * @param params    请求的查询参数,可以为null 
         * @param charset 字符集 
         * @param pretty    是否美化 
         * @return 返回请求响应的HTML 
         */ 
        public static String doPost(String url, Map<String, String> params, String charset, boolean pretty) {

            StringBuffer response = new StringBuffer(); 
            HttpClient client = new HttpClient(); 
            PostMethod method = new PostMethod(url); 
            //设置Http Post数据 
            if (params != null) { 
                    for (Map.Entry<String, String> entry : params.entrySet()) { 
								method.setParameter(entry.getKey(),entry.getValue());
                    } 
                    
            } 
            try { 
            	    method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
                    client.executeMethod(method); 
                    if (method.getStatusCode() == HttpStatus.SC_OK) { 
                            BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset)); 
                            String line; 
                            while ((line = reader.readLine()) != null) { 
                                    if (pretty) 
                                            response.append(line).append(System.getProperty("line.separator"));
                                    else 
                                            response.append(line); 
                            } 
                            reader.close(); 
                    } 
            } catch (IOException e) { 
                  //  log.error("执行HTTP Post请求" + url + "时，发生异常！", e); 
            } finally { 
                    method.releaseConnection(); 
            } 
            return response.toString(); 
    } 
        
    	public static String doHttpPost(String uri, String queryString,String ecode)
			throws ServletException, IOException {
		URL url = null;
		HttpURLConnection httpConn = null;
		OutputStream output = null;
		OutputStreamWriter outr = null;

		url = new URL(uri);
		httpConn = (HttpURLConnection) url.openConnection();
		HttpURLConnection.setFollowRedirects(true);
		httpConn.setDoOutput(true);
		httpConn.setRequestMethod("POST");
		// httpConn.setRequestProperty("Content-Type",
		// "text/plain; charset=utf-8");
		// httpConn.setRequestProperty("Content-Type",
		// "application/octet-stream");
		// httpConn.setRequestProperty("Accept-Charset", "gb2312");
		// httpConn.setRequestProperty("contentType", "gb2312");
		httpConn.connect();
		output = httpConn.getOutputStream();
		outr = new OutputStreamWriter(output,ecode);
		// 写入请求参数
		outr.write(queryString.toCharArray(), 0, queryString.length());
		outr.flush();
		outr.close();
		int code = httpConn.getResponseCode();
		// 读取响应内容
		String sCurrentLine = "";
		String sTotalString = "";
		if (code == 200) {
			java.io.InputStream is = httpConn.getInputStream();
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(is,ecode));
			
//			 BufferedReader in = new BufferedReader(new InputStreamReader(inStream, "GBK"));  
			while ((sCurrentLine = reader.readLine()) != null)
				if (sCurrentLine.length() > 0)
					sTotalString = sTotalString + sCurrentLine.trim();
		} else {
			sTotalString = "远程服务器连接失败,错误代码:" + code;

		}
		return sTotalString;
	}
        

	public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
//		Thread1 mTh1=new Thread1(100);  
//        Thread1 mTh2=new Thread1(1200);  
//        mTh1.start();  
//        mTh2.start(); 
		concatInfo(332);
	//	System.out.println(ImgHelper.encodeImage("d:\\12.png"));

	} 
	
	public static class Thread1 extends Thread{  
	    private int in;  
	    public Thread1(int i) {  
	       this.in=i;  
	    }  
	    public void run() {  
	        for (int i = in,j=0; j < 50; i++,j++) {  
	            System.out.println(in + "运行  :  " + i);  
	            try {  
	            	concatInfo(i);
	            } catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
	        }  
	         
	    }  
	}  
	
	public static void concatInfo(int i) throws IOException{
		
		Map m=new HashMap();
		
		List<Map> list=new ArrayList<Map>();
		
		Map<String,Object> map=new HashMap<String, Object>();
//		map.put("zfbh", "8747"+i);
//		map.put("zfxm", "长城"+i);
//		map.put("xmpy", "huofang");
//		map.put("szjy", "鼓楼区司法所");
//		map.put("rjrq", "2017-05-15");
//		map.put("drrq", "2017-05-15");
//		map.put("zfcxm", "小货");
//		map.put("xb", "男");
//		map.put("mz", "汉族");
//		map.put("zjlb", "身份证");
//		map.put("zjhm", "350101900092938472");
//		map.put("pqzy", "无业");
//		map.put("pqwhcd", "高中");
//		map.put("jg", "福州台江区");
//		map.put("hjdqh", "350506");
//		map.put("hjdxx", "福建省福州市台江区白马南路110号");
//		map.put("jtdqh", "350102");
//		map.put("jtdxx", "福建省福州市台江区白马南路110号");
//		map.put("zzmm", "中共党员");
//		map.put("hyzk", "未婚");
//		map.put("jkzk", "良好");
//		map.put("pqzw", "新西方科技有限公司总裁");
//		map.put("jszc", "");
//		map.put("hklx", "城镇户口");
//		map.put("sg", "176");
//		map.put("tz", "85");
//		map.put("xx", "B");
//		map.put("ky", "福州口音");
//		map.put("xmtz", "卧蚕眉，丹凤眼");
//		map.put("tc", "股票投资");
//		map.put("cyxj", "");
//		map.put("zjxy", "基督教");
//		map.put("jlrq", "2017-03-15");
//		map.put("dbrq", "2017-02-15");
//		map.put("dbjg", "鼓楼区公安局");
//		map.put("ysrq", "2017-04-15");
//		map.put("ysjg", "福州市中级人民法院");
//		map.put("yszh", "（2013）鼓刑初字第528号");
//		map.put("zsrq", "2017-05-11");
//		map.put("zsjg", "福建省高级人民法院");
//		map.put("zszh", "（2013）鼓刑初字第528号");
//		map.put("zm", "侵犯公民人身权利、民主权利罪");
//		map.put("xq", "有期徒刑三年，缓刑四年");
//		map.put("xqqr", "2017-05-15");
//		map.put("xqzr", "2017-06-19");
//		map.put("fjx", "罚金20万元");
//		map.put("aflx", "一般刑事犯");
//		map.put("fylx", "暴力型");
//		map.put("fzss", "本院认为，被告人陈杨柳以非法占有为目的，采用秘密手段窃取他人财物，价值人民币45600元，被告人行为已构成盗窃罪。");
//		map.put("jl", "1980年09月至1986年07月 成功小学");
//		map.put("bz", "无");
//		map.put("sslx", "涉枪");
//		map.put("zyfz", "");
//		map.put("flg", "否");
//		map.put("zdzf", "一般刑事犯");
//		map.put("cjlx", "解矫");
//		map.put("cjrq", "2017-06-20");
//		map.put("cjqx", "在家");
//		
//		Map ms=new HashMap();
//		ms.put("gx", "父亲"+i);
//		ms.put("gxxm", "李伟"+i);
//		ms.put("szdw", "新西方科技有限公司");
//		ms.put("jtdxx", "福州市仓山区仓山路177号");
//		ms.put("zjlx", "身份证");
//		ms.put("zjhm", "350181099876768142");
//		ms.put("dhhm", "0591-9988798");
//		ms.put("sjhm", "18909893625");
//		
//		list.add(ms);
//		
//		Map mq=new HashMap();
//		mq.put("gx", "母亲"+i);
//		mq.put("gxxm", "霍英文"+i);
//		mq.put("szdw", "新西方科技有限公司");
//		mq.put("jtdxx", "福州市仓山区仓山路177号");
//		mq.put("zjlx", "身份证");
//		mq.put("zjhm", "3501810998767633342");
//		mq.put("dhhm", "0591-9988798");
//		mq.put("sjhm", "18909893555");
//		
//		list.add(mq);
//		
//		map.put("socialrelation", list);
//		

	//	map.put("photo", ImgHelper.encodeImage("d:\\2.png"));
		
		ObjectMapper mapper = new ObjectMapper();
//		String p=mapper.writeValueAsString(map);
	//	System.out.println(ImgHelper.encodeImage("d:\\2.png"));
		
		
//		SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
//		String str="azbj."+sd.format(new Date());
//		m.put("TOKEN", MD5.md5(str));
		//http://127.0.0.1:8080/ccms-ic/FaceSign/nodetectface.do
		//http://59.60.28.232:9102/ccms-ic/FaceSign/usersign.do
		m.put("suffix","JPG");
		m.put("picture","data:image/jpeg;base64,"+ImgHelper.encodeImage("d:\\IMG_0702.JPG"));
		
		String a=doPost("http://127.0.0.1:8002/socialclass/interface/fileUpload",m,"utf-8",true);
		System.out.println(a);
		
	}
}